/**
 * 
 */
package com.toncen.samepms.common;

import java.util.Set;

import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.system.domain.Advice;
import com.sky.common.email.EmailUtils;
import com.sky.common.string.StringUtils;

/**
 * 描述：邮件通知发布者
 * 
 * @author tch
 * 
 */
public class EmailAdviceDispatcher extends AbstractAdviceDispatcher {
	/**
	 * 通知方式
	 */
	private static final int MODE = 1;

	/**
	 * 
	 */
	public EmailAdviceDispatcher() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#accept(com.toncen.samepms.basic.domain.User,
	 *      com.toncen.samepms.system.domain.Advice)
	 */
	public boolean accept(User user, Advice advice) {
		boolean isTrue = false;
		if (!StringUtils.isEmpty(user.getEmail())) {
			isTrue = true;
		}
		return isTrue;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#send(com.toncen.samepms.system.domain.Advice,
	 *      com.toncen.samepms.basic.domain.User, java.util.Set, java.lang.String,
	 *      java.lang.String)
	 */
	public int send(Advice advice, User fromUser, Set<User> toUsers, String subject, String content) throws Exception {
		int result = VOID;
		for (User toUser : toUsers) {
			if (accept(toUser, advice)) {// 接收者邮箱存在
				result = send(toUser, getWrapSubject(fromUser, toUser, subject), getWrapContent(fromUser, toUser, content)) ? SUCCESSFUL
						: UNSUCCESSFUL;
			}
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#getMode()
	 */
	public int getMode() {
		return MODE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAdviceDispatcher#send(java.lang.Object,
	 *      java.lang.String, java.lang.String)
	 */
	@Override
	public boolean send(User target, String subject, String content) throws Exception {
		return EmailUtils.sendMail(target.getEmail(), subject, content + "\n此信由系统自动发出，勿需回复。");
	}
}
